package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.ldgs.MainMudjack;
import com.app.domain.ldgs.MudjackData;
import com.app.domain.ldgs.MudjackProcessData;
import com.app.domain.ldgs.ResponseData;
import com.app.utils.HttpClients;
import com.app.utils.MathUtil;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * @author ：wangyu
 * @date ：Created in 2024/8/23 10:06
 * 常州常鑫路桥预制构件厂
 */
@Service

public class LdgsService {

    @Autowired
    YjDataService yjDataService;
    @Transactional(readOnly = true)
    public MainMudjack getMainMudjack(List<YjData> yjData, UploadParameter uploadParameter) {
        MainMudjack mainMudjack = new MainMudjack();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        mainMudjack.setMainId(uuid);
        mainMudjack.setUkey(uploadParameter.getShebeibianhao() + uuid);
        mainMudjack.setEquipmentCode(uploadParameter.getShebeibianhao());
        mainMudjack.setConstructionUnit(uploadParameter.getSgdw());
        mainMudjack.setSupervisionUnit(uploadParameter.getJldw());
        mainMudjack.setContractNumber(uploadParameter.getHtbh());
        mainMudjack.setProjectName(uploadParameter.getGcmc());
        mainMudjack.setBeamFieldName("零道高速压浆八标一号梁场");
        mainMudjack.setMudjackTime(yjData.get(0).getMudjack_date() + " " + yjData.get(0).getBegin_time());
        mainMudjack.setFinallyTime(uploadParameter.getZlsj());
        mainMudjack.setStakeNumberPosition(uploadParameter.getZhbw());
        mainMudjack.setConstructionPosition(uploadParameter.getZhbw());
        mainMudjack.setComponentStructure(yjData.get(0).getBridge_code());
        mainMudjack.setComponentCode(yjData.get(0).getBridge_code());
        mainMudjack.setTemperature(yjData.get(0).getEnvironment_temperature());
        mainMudjack.setWaterDosage(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getCjsjl()));
        mainMudjack.setInflationDosage(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getCpzjl()));
        mainMudjack.setWaterTemperature(yjData.get(0).getSlurry_temperature());
        mainMudjack.setWaterBinderRatio(yjData.get(0).getWater_binder_ratio());
        mainMudjack.setCementDosage(yjData.get(0).getPractical_mudjack_value());
        mainMudjack.setMudjackTemperature(yjData.get(0).getSlurry_temperature());
        mainMudjack.setExudationRate(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getMsl()));
        mainMudjack.setSpare("");
        mainMudjack.setMudjackDeviceCode(uploadParameter.getShebeibianhao());
        mainMudjack.setBeamSlabType(uploadParameter.getLblx());
        mainMudjack.setBeamNumber(yjData.get(0).getBridge_code());
        mainMudjack.setTensionTime(uploadParameter.getZlsj());
        mainMudjack.setMudjackAgentManufacturer(uploadParameter.getYajiangji());
        mainMudjack.setMudjackAgentSpecification(uploadParameter.getYajiangji());
        mainMudjack.setMudjackAgentBatch(uploadParameter.getYajiangji());
        mainMudjack.setCementSpecification(uploadParameter.getSnmc());
        mainMudjack.setCementBatch(uploadParameter.getSnmc());
        mainMudjack.setCementManufacturer(uploadParameter.getSnmc());
        mainMudjack.setHoleChannelsNumber(uploadParameter.getKongdaoshu());
        mainMudjack.setMudjackDirection(uploadParameter.getYajiangfangxiang());
        mainMudjack.setMudjackStep(uploadParameter.getYajiangbuzhou());
        mainMudjack.setMudjackProcess(uploadParameter.getYajiangmoshi());
        mainMudjack.setInitialFlowVelocity(yjData.get(0).getPractical_fluidity());
        mainMudjack.setFluidity(yjData.get(0).getPractical_fluidity());
        mainMudjack.setMemo(uploadParameter.getMemo());
        mainMudjack.setStatus(Integer.valueOf(uploadParameter.getStatus()));
        mainMudjack.setSpan("");
        return mainMudjack;
    }

    public List getMudjackData(List<YjData> yjDataList, UploadParameter uploadParameter, String mainId) {
        List<MudjackData> mudjackDataList = new ArrayList<>();
        for (YjData yjData : yjDataList) {
            MudjackData mudjackData = new MudjackData();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            mudjackData.setDataId(uuid);
            mudjackData.setUkey(uploadParameter.getShebeibianhao() + uuid);
            mudjackData.setMainId(mainId);
            mudjackData.setMudjackTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            mudjackData.setHoleChannels(yjData.getDuct_code());
            mudjackData.setChannelDiameter("30");
            mudjackData.setMudjackMode(uploadParameter.getYajiangmoshi());
            mudjackData.setMixProportion(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getPhb()));
            mudjackData.setWaterBinderRatio(yjData.getWater_binder_ratio());
            mudjackData.setStirTime(MathUtil.roundToTwoDecimalPlaces(yjData.getPressurize_time()));
            mudjackData.setStartTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            mudjackData.setEndTime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            mudjackData.setIntoSlurryPressure(yjData.getMudjack_pressure());
            mudjackData.setBackSlurryPressure(yjData.getSlurry_pressure());
            mudjackData.setDuration(MathUtil.roundToTwoDecimalPlaces(yjData.getPressurize_time()));
            mudjackData.setIntoSlurryVolume(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getJinjiangshuliang()));
            mudjackData.setBackSlurryVolume(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getFanjiangliang()));
            mudjackData.setVacuumPumpPressure(MathUtil.roundToTwoDecimalPlaces(uploadParameter.getPumppressure()));
            mudjackData.setPass("通过");
            mudjackData.setMudjackNumber(yjDataList.size());
            mudjackData.setQualifiedOrNot(1);
            mudjackData.setHoleId(uuid);
            mudjackData.setStatus(Integer.valueOf(uploadParameter.getStatus()));
            mudjackData.setMemo(String.valueOf(yjData.getId()));
            mudjackData.setDisposalSituation("");
            mudjackData.setDwellTime(yjData.getPressurize_time());
            mudjackData.setTheoryMudjackVolume(yjData.getTheory_volume());
            mudjackData.setRealMudjackVolume(yjData.getPractical_volume());
            mudjackData.setMudjackVolumeError(0.0);
            mudjackData.setPressureErrorWarning(0);
            mudjackData.setDwellTimeWarning(0);
            mudjackData.setBinderRatioWarning(0);
            mudjackDataList.add(mudjackData);

        }
        return mudjackDataList;
    }

    public List<MudjackProcessData> getMudjackProcessData(MudjackData mudjackData, UploadParameter uploadParameter) {
        List<MudjackProcessData>mudjackProcessDataList=new ArrayList<>();
        Map<String, List> graphData = yjDataService.getGraphData(String.valueOf(mudjackData.getMemo()));
        List<String> dateList = graphData.get("dateList");
        List<String> mcgList = graphData.get("mcgList");
        for (int i = 0; i < mcgList.size(); i++) {
            MudjackProcessData mudjackProcessData = new MudjackProcessData();
            String uuid = UUID.randomUUID().toString().replace("-", "");
            mudjackProcessData.setProcessDataId(uuid);
            mudjackProcessData.setUkey(uploadParameter.getShebeibianhao() + uuid);
            mudjackProcessData.setEquipmentCode(uploadParameter.getShebeibianhao());
            mudjackProcessData.setHoleId(mudjackData.getHoleId());
            mudjackProcessData.setRecordTime(mudjackData.getStartTime().split(" ")[0]+" "+ dateList.get(i));
            mudjackProcessData.setHoldPressureTime(5);
            mudjackProcessData.setStatus("正常");
            mudjackProcessData.setIntoSlurryPressure(MathUtil.roundToTwoDecimalPlaces(mcgList.get(i)));
            mudjackProcessData.setBackSlurryPressure(0.0);
            mudjackProcessData.setIntoSlurryVolume(0.0);
            mudjackProcessData.setBackSlurryVolume(0.0);
            mudjackProcessDataList.add(mudjackProcessData);
        }
        return mudjackProcessDataList;
    }
    public boolean updateState(String state, String id) {
        // 参数校验
        if (state == null || id == null || state.isEmpty() || id.isEmpty()) {
            return false;
        }
        try {
            int i = yjDataService.updateStateById(state, id);
            boolean result = i > 0;
            return result;
        } catch (Exception e) {
            return false;
        }
    }

    public ResponseData getToken(String url, Map<String, Object> params) throws Exception {
        String resp = HttpClients.doPost(url, params);
        ResponseData responseData = (ResponseData) JSON.parseObject(StringUtil.replaceBlank(resp), ResponseData.class);
//        AuthToken result = (AuthToken) responseData.getResult();
        return responseData;
    }

    public ResponseData sendData(String url, String json, Map<String, String> params) throws Exception {
        String resp = HttpClients.sendPost(url, json, params);
        ResponseData responseData = (ResponseData) JSON.parseObject(StringUtil.replaceBlank(resp), ResponseData.class);
        return responseData;
    }



}
